package com.zjl.service.impl;

import com.zjl.domain.WalletRecord;
import com.zjl.mapper.WalletRecordMapper;
import com.zjl.service.WalletRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author EternalPain
 * @Date 2023/1/31 18:46
 * @Version 1.0
 */
@Service
public class WalletRecordServiceImpl implements WalletRecordService {

    @Resource
    public WalletRecordMapper recordMapper;


    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean addRecord(WalletRecord record) {
        return recordMapper.addRecord(record);
    }


    @Override
    public String findRecord(int userId) {
        List<WalletRecord> records = recordMapper.findRecord(userId);

        // 判断是否为空
        if (ObjectUtils.isEmpty(records)) {
            return "未查询到用户钱包金额变动明细";
        }

        StringBuilder builder = new StringBuilder();

        for (WalletRecord record : records) {
            // 判断是充值还是消费
            builder.append(record.isOperation() ? "+" : "-");
            // 金额
            builder.append(record.getMoney());

            builder.append("&nbsp;&nbsp;&nbsp;&nbsp;");

            if (record.isRefund()) {
                builder.append("已退款");
            }

            builder.append("<br>");
        }

        return builder.toString();
    }
}
